<template>
  <a-modal
    :title="title"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
    :maskClosable="false"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row>
          <a-col :span="12">
            <a-form-item label="学号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                placeholder="请输入学号"
                v-decorator="['studentNo', { rules: [{ required: true, message: '请输入学号!' }] }]"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="姓名" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                placeholder="请输入姓名"
                v-decorator="['name', { rules: [{ required: true, message: '请输入姓名!' }] }]"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="性别" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-select
                placeholder="请选择性别"
                v-decorator="['gender', { rules: [{ required: true, message: '请选择性别!' }] }]"
              >
                <a-select-option value="男">男</a-select-option>
                <a-select-option value="女">女</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="身份证号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                placeholder="请输入身份证号"
                v-decorator="['idCard', { rules: [{ pattern: /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/, message: '请输入正确的身份证号!' }] }]"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="联系电话" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                placeholder="请输入联系电话"
                v-decorator="['phone', { rules: [{ pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码!' }] }]"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="学生状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-select
                placeholder="请选择状态"
                v-decorator="['status', { rules: [{ required: true, message: '请选择状态!' }] }]"
              >
                <a-select-option value="在校">在校</a-select-option>
                <a-select-option value="离校">离校</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="院系" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                placeholder="请输入院系"
                v-decorator="['college']"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="专业" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                placeholder="请输入专业"
                v-decorator="['major']"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="年级" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                placeholder="请输入年级"
                v-decorator="['grade']"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="班级" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                placeholder="请输入班级"
                v-decorator="['className']"
              />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from 'moment'

  export default {
    name: 'DormitoryStudentModal',
    data () {
      return {
        form: this.$form.createForm(this),
        title: '操作',
        width: 800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        confirmLoading: false,
        url: {
          add: '/dormitory/student/add',
          edit: '/dormitory/student/edit',
        }
      }
    },
    created () {
    },
    methods: {
      // 打开表单
      add () {
        this.edit({});
      },
      // 编辑表单
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, [
            'studentNo',
            'name',
            'gender',
            'idCard',
            'phone',
            'status',
            'college',
            'major',
            'grade',
            'className'
          ]))
        });
      },
      // 确定
      handleOk () {
        const that = this;
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpUrl = '';
            let method = '';
            if (!this.model.id) {
              httpUrl = this.url.add;
              method = 'post';
            } else {
              httpUrl = this.url.edit;
              method = 'put';
              values.id = this.model.id;
            }
            console.log(values)
            httpAction(httpUrl, values, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
        })
      },
      // 关闭
      handleCancel () {
        this.close();
      },
      close () {
        this.$emit('close');
        this.visible = false;
      }
    }
  }
</script> 